/*
 * BuscaServlet.java
 *
 * Created on 16 de Junho de 2007, 15:58
 */

package action;

import java.io.*;
import java.net.*;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.*;
import javax.servlet.http.*;
import model.persistence.dao.FilmeDAO;
import model.persistence.dao.HibernateDAO;
import model.persistence.vo.Cinema;
import model.persistence.vo.Filme;

/**
 *
 * @author $Leandro$
 * @version
 */
public class BuscaCinemasServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request,response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        String strIdBairro = (String) request.getParameter("idBairro");
        
        DataOutputStream out = new DataOutputStream(response.getOutputStream());
        
        int idBairro = Integer.parseInt(strIdBairro);
        
        FilmeDAO fDao = new FilmeDAO();
        Collection result = fDao.getCinemasByRegiao(idBairro);
        
        Iterator it = result.iterator();
        
        out.writeInt(result.size());
        
        while(it.hasNext()) {
            Cinema c = (Cinema) it.next();
            try {
                out.write(c.serialize());
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
    }
}